import TreeTraversal.TreeNode;

public class buildTree {
    private int pre = 0;
    private int in = 0;
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        return a(preorder,inorder,Integer.MAX_VALUE+1);
    }
    public TreeNode a(int[] preorder,int[] inorder,long stop){
        if(pre == preorder.length){
            return null;
        }
        if(inorder[in] == stop){
            in++;
            return null;
        }
        int val = preorder[pre];
        pre++;
        TreeNode root = new TreeNode(val);
        root.left = a(preorder,inorder,val);
        root.right = a(preorder,inorder,stop);
        return root;
    }
}
